<?php
/**
 * Created by PhpStorm.
 * User: xiong <1099446868@qq.com>
 * DateTime: 2018/12/22
 * TIME: 16:32
 */

namespace app\mobile\controller;
use app\mobile\model\Mobile as MobileModel;
use app\index\model\Book as BookModel;

class Book extends BaseController{

    public function search($searchname){
        $searchname = addslashes($searchname);
        $book = BookModel::getBookByBookName($searchname);
        if(!$book){
            return '暂无本书数据，或者联系站长1099446868@qq.com添加';
        }
        $table = switchTableById2($book['id']);
        $book['end'] = BookModel::getEnd5Chapter($book['id'], $table);


        if (array_key_exists('id', $book) === true) {
            $this->assign('config', config('xpath')['zh']['mobile']['lanmutuijian']);
            $this->assign('book', $book);
            $this->assign('id', $book['id']);
            return $this->fetch('mobile/book');
        } else {
            //dump($book['books']);exit;
            $this->assign('book', $book);
            return $this->fetch('search');
        }
    }

    public function book(int $id){
        $table = switchTableById2($id);
        $book = BookModel::getBookById($id, $table);
        $book['end'] = BookModel::getEnd5Chapter($id, $table);
        $this->assign('book', $book);
        //dump($book);

        return $this->fetch('mobile/book');
    }

    public function catalog(int $id){
        $book = MobileModel::getBookById($id);
        $this->assign('book', $book);

        $table = switchTableById2($id);
        $catalog = MobileModel::getCatalogsByBid($id, $table);
        $count = MobileModel::getCatalogCount($id, $table);
        //dump($catalog);
        $this->assign('count', $count);
        $this->assign('book', $book);
        $this->assign('table', $table);
        $this->assign('catalog', $catalog);

        return $this->fetch('mobile/catalog');
    }

    public function content(int $bid, int $id) {

        $table = switchTableById2($bid);
        $book = MobileModel::Content($bid, $id, $table);
        $catalog = url('/catalog/' . $book['id']);
        $info = url('/book/' . $book['id']);
        $this->assign('info', $info);
        $this->assign('book', $book);
        $this->assign('catalog', $catalog);

        return $this->fetch('mobile/content_zh');
    }

    public function find(){
        $request = Request::instance();
        $param = $request->param();

        if (!isset($param['keyword'])) {
            $this->redirect("http://www.xiaojuan.fun", 301);
        }
        $find = Db::name('bookname')
            ->where('bookname|author', 'like', '%' . $param['keyword'] . '%')
            ->paginate(20, false,['query'=>$param]);

        $this->assign('page', $find->render());
        $this->assign('book', $find);
        $this->assign('count', $find->total());
        $this->assign('searchname', $param['keyword']);

        //return $this->fetch('findToSearch');
    }
}